package com.imu.purse.modules.trade.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.imu.core.base.BaseMapper;
import com.imu.purse.modules.bitcoin.form.BitcoinForm;
import com.imu.purse.modules.trade.entity.FundEntity;

/**
 * 
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-31 12:04:14
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
public interface FundMapper extends BaseMapper<FundEntity>
{
	/**
     * 分页查询
     * @param entity 页号
     * @return
     */
	@Select({
		"<script>",
		    "SELECT i.id,",
					"i.user_id, ",
					"i.money, ",
					"i.lock_money, ",
					"i.money_address, ",
					"i.time_create, ",
					"i.time_update, ",
					"i.FROZEN_MONEY, ",
					"iu.phone ",
		    "FROM imu_fund i ",
		    "left join imu_user iu on i.user_id=iu.id ",
		    "WHERE 1=1",
		    "<when test = 'timeUpdate != null'>",
	    	"AND i.time_create &gt;= #{timeUpdate}",
		    "</when>",
		    "<when test = 'timeCreate != null'>",
			"AND i.time_create &lt;= #{timeCreate}",
			"</when>",
			"<when test = 'userId != null'>",
			"AND i.USER_ID = #{userId}",
			"</when>",
		    "ORDER BY id DESC",
    	"</script>"
	})
    public List<FundEntity> findByPage(final FundEntity entity);
	
	
	/****
	 * 更新用户钱包
	 * @param entity
	 */
	@Update({"update imu_fund "
	 		+ "set money = IFNULL(money,0) + #{money},"
	 		+     "lock_money = lock_money + #{money},"
	 		+     "time_update = #{timeUpdate} "
	 		+ "where user_id = #{userId}" })
	public int upMoney(final FundEntity entity);
	
	/****
	 * 更新用户钱包
	 * @param entity
	 */
	@Update({"update imu_fund "
	 		+ "set money = IFNULL(money,0) + #{money},"
	 		+     "time_update = #{timeUpdate} "
	 		+ "where user_id = #{userId}" })
	public int moMoney(final FundEntity entity);
	
	/**
	 * 根据用户ID获取数据
	 * */
	@Select({"<script>",
		    "SELECT id,",
					"user_id, ",
					"money, ",
					"lock_money, ",
					"frozen_money, ",
					"money_address, ",
					"time_create, ",
					"time_update, ",
					"password, ",
					" bj, ",
					" CAST(btc as SIGNED) as btc, ",
					" fcg, ",
					" hbc, ",
					" hbb, ",
					" ico, ",
					" eth, ",
					" fen, ",
					" cowry, ",
					" LOCK_BJ , ",
					" LOCK_FCG, ",
					" LOCK_ICO, ",
					" LOCK_ETH, ",
					" LOCK_COWRY ",
					"FROM imu_fund ",
		    "WHERE user_id = #{userId}",
			
    	"</script>"})
	public FundEntity findByUserId(@Param("userId") final Long userId);
	
	/**
	 * 更新冻结钱包
	 * @param entity
	 * @return
	 */
	@Update({
		"update imu_fund "
		 		+ "set frozen_money = IFNULL(frozen_money,0) + #{frozenMoney},"
		 		+     "time_update = #{timeUpdate} "
		 		+ "where user_id = #{userId}" 
	})
	public int moFrozenMoney(final FundEntity entity);


	/**
	 * 不同核心钱包转账，针对转出人
	 * @param form
	 * @return
	 */
	@Update({
		"update imu_fund set money = IFNULL(money,0) + #{amount} + #{fee}," +
				"frozen_money = IFNULL(frozen_money,0) + #{amount} + #{fee}," +
				"time_update = FROM_UNIXTIME(#{curTime}) "
				+ " where user_id = #{userId}"
	})
	public int modByUId(BitcoinForm form);

	/**
	 * 不同核心钱包转账，针对转入人
	 * @param form
	 * @return
	 */
	@Update({
			"update imu_fund set money = IFNULL(money,0) + #{amount} + #{fee}," +
					"time_update = FROM_UNIXTIME(#{curTime}) "
					+ " where user_id = #{userId}"
	})
	public int modNoByUId(BitcoinForm form);


	/**
	 * 同一个核心钱包间转账
	 * @param entity
	 * @return
	 */
	@Update({
			"update imu_fund set money = IFNULL(money,0) + #{money}," +
					" time_update = #{timeUpdate} "
					+ " where user_id = #{userId}"
	})
	public int modMoneyByUId(final FundEntity entity);
	
	/**
	 * 内部转账修改金额（转出）
	 * @param entity
	 * @return
	 */
	@Update({
		"update imu_fund "
		 		+ "set money = IFNULL(money,0) - #{money} "
		 		+ "where user_id = #{userId}" 
	})
	public int reduceMoney(final FundEntity entity);
	/**
	 * 内部转账修改金额（转入）
	 * @param entity
	 * @return
	 */
	@Update({
		"update imu_fund "
		 		+ "set money = IFNULL(money,0) + #{money} "
		 		+ "where user_id = #{userId}" 
	})
	public int addMoney(final FundEntity entity);

	/**
	 * 合营下单，扣取20%Imu，80% GET
	 * @param entity
	 * @return
	 */
	@Update({
			"update imu_fund set money = IFNULL(money,0) - #{money},lock_eth = #{locketh} - #{locketh}" +
					",time_update = #{timeUpdate} " +
					"where user_id = #{userId}" +
					" and IFNULL(money,0) - #{money} >=0 and IFNULL(lock_eth,0) - #{locketh} >=0"
	})
	public int updateMoneyByUId(final FundEntity entity);


	/**
	 * 根据用户ID和类型修改不同类型的金额
	 * @param entity
	 * @return
	 */
	@Update(
		{
		 "<script>",
			"update imu_fund " ,
				" <set> ",
					"<if test=\"ytfType == 0\">money = IFNULL(money,0) + #{money},</if>\n" ,
					"<if test=\"ytfType == 2\">bj = IFNULL(bj,0) + #{money} ,</if>\n" ,
					"<if test=\"ytfType == 3\">LOCK_ICO = IFNULL(LOCK_ICO,0) + #{money},</if>\n",
					"<if test=\"ytfType == 4\">LOCK_ETH = IFNULL(LOCK_ETH,0) + #{money},</if>\n" ,
					"<if test=\"ytfType == 5\">lock_cowry = IFNULL(lock_cowry,0) + #{money},</if>\n" ,
					"<if test=\"ytfType == 6\">hbc = IFNULL(hbc,0) + #{money},</if>\n" ,
					" time_update = #{timeUpdate} ",
				"</set>" +
			" where user_id = #{userId}",
			"</script>"
		}
	)
	public int updateMoneyByUIdAndYtype(final FundEntity entity);
	
	/****
	 * 更新用户钱包
	 * @param entity
	 */
	@Update({
		"<script>",
			"update imu_fund" ,
				"<set>",
					"<if test='null != money and 0 != money'>money = IFNULL(money,0) + #{money},</if>\n" ,
					"<if test='null != bj and 0 != bj'>bj = IFNULL(bj,0) + #{bj} ,</if>\n" ,
					"<if test='null != locketh and 0 != locketh'>lock_eth = IFNULL(lock_eth,0) + #{locketh},</if>\n" ,
					"<if test='null != lockcowry and 0 != lockcowry'>lock_cowry = IFNULL(lock_cowry,0) + #{lockcowry},</if>\n" ,
					"time_update = #{timeUpdate} ",
				"</set>" +
			"where user_id = #{userId}",
		"</script>"
	})
	public int up(final FundEntity entity);
	
	/**
	 * 私募修改
	 * @param entity
	 * @return
	 */
	@Update({
		"<script>",
			"update imu_fund" ,
				"<set>",
					"<if test='null != money and 0 != money'>money = IFNULL(money,0) + #{money},</if>\n" ,
					"<if test='null != bj and 0 != bj'>bj = IFNULL(bj,0) + #{bj} ,</if>\n" ,
					"<if test='null != ico and 0 != ico'>ico = IFNULL(ico,0) + #{ico},</if>\n",
					"<if test='null != locketh and 0 != locketh'>lock_eth = IFNULL(lock_eth,0) + #{locketh},</if>\n" ,
					"<if test='null != lockcowry and 0 != lockcowry'>lock_cowry = IFNULL(lock_cowry,0) + #{lockcowry},</if>\n" ,
					"<if test='null != hbc and 0 != hbc'>hbc = IFNULL(hbc,0) + #{hbc},</if>\n" ,
					"time_update = #{timeUpdate} ",
				"</set>" +
			"where user_id = #{userId}",
		"</script>"
	})
	public int upPri(final FundEntity entity);

	/**
	 * 修改以太坊钱包地址
	 * @param entity
	 * @return
	 */
	@Update({
			"update imu_fund set money_address = #{moneyAddress},password = #{password}" +
					",time_update = #{timeUpdate} where id = #{id}"

	})
	public int upAddressById(final FundEntity entity);
}
